/**********************************************************
 * CustomFieldsManagerAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:46:55 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.CustomFieldsFormatsDelegator;
import com.printeverywhere.dto.CustomFields;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.util.AdminConstants;

/**
 * The Class CustomFieldsManagerAction.
 */
public class CustomFieldsManagerAction extends AdminSystemManagement implements ServletRequestAware{

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(CustomFieldsManagerAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	private List<Languages> languagesList;

	private List<CustomFields> customFieldsFormatsList = new ArrayList<CustomFields>();

	private CustomFields customFieldsFormats;

	private Integer newFormat;

	private Integer deleteFormat;

	private Integer customType;

	private String languageCode;

	private HttpServletRequest request;
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}
		try {
			if(request.getParameter(AdminConstants.ID)!= null){
				customFieldsFormats = new CustomFields();
				customFieldsFormats = (CustomFields) CommonFunctionDelegator.getObjectById(AdminConstants.ID, Integer.parseInt(request.getParameter(AdminConstants.ID)), CustomFields.class);
			}
			languagesList = (List<Languages>) ActionContext.getContext().getApplication().get(AdminConstants.LANGUAGES);
			customFieldsFormatsList = CustomFieldsFormatsDelegator.getList();
		} catch (Exception e) {
			return INPUT;
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String submitCustomFieldsManager(){
		try {
			if((newFormat != null) && (request.getParameter("create")!= null) && (request.getParameter("create").equals("copyFormat"))){
				CustomFields customFieldsFormatsTemp = (CustomFields) CommonFunctionDelegator.getObjectById(AdminConstants.ID, newFormat, CustomFields.class);
				customFieldsFormatsTemp.setId(null);
				customFieldsFormatsTemp.setName(customFieldsFormats.getName());
				CustomFieldsFormatsDelegator.add(customFieldsFormatsTemp);
				addActionMessage("New custom field created.");
			}
			if((deleteFormat != null) && (request.getParameter("delete")!= null) && (request.getParameter("delete").equals("delete"))){
				CustomFields customFieldsFormatsTemp = (CustomFields) CommonFunctionDelegator.getObjectById(AdminConstants.ID, deleteFormat, CustomFields.class);
				CustomFieldsFormatsDelegator.delete(customFieldsFormatsTemp);
				addActionMessage("Custom field deleted.");
			}
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

		public String updateCustomFieldsManager(){
		try {
			if(customFieldsFormats.getId() != null){
				CustomFieldsFormatsDelegator.update(customFieldsFormats);
				addActionMessage("Record Updated successfully");
			}else{
				CustomFieldsFormatsDelegator.add(customFieldsFormats);
				addActionMessage("Record added successfully");
			}
			customFieldsFormatsList = CustomFieldsFormatsDelegator.getList();
		} catch (Exception e) {
			return INPUT;
		}
		return SUCCESS;
	}

	@Override
	public void validate() {
		try{
			if((customFieldsFormats.getName().equals(AdminConstants.NONE)) || (customFieldsFormats.getName() == null)){
				addActionError("Please, enter name.");
			}
		}catch (Exception e) {
			addActionError("Problem in displaying customfields.");
		}finally{
			languagesList = (List<Languages>) ActionContext.getContext().getApplication().get(AdminConstants.LANGUAGES);
		}
	}

	public CustomFields getCustomFieldsFormats() {
		return customFieldsFormats;
	}

	public void setCustomFieldsFormats(CustomFields customFieldsFormats) {
		this.customFieldsFormats = customFieldsFormats;
	}

	public Integer getCustomType() {
		return customType;
	}

	public void setCustomType(Integer customType) {
		this.customType = customType;
	}

	public List<CustomFields> getCustomFieldsFormatsList() {
		return customFieldsFormatsList;
	}

	public void setCustomFieldsFormatsList(
			List<CustomFields> customFieldsFormatsList) {
		this.customFieldsFormatsList = customFieldsFormatsList;
	}

	public Integer getNewFormat() {
		return newFormat;
	}

	public void setNewFormat(Integer newFormat) {
		this.newFormat = newFormat;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;
	}

	public Integer getDeleteFormat() {
		return deleteFormat;
	}

	public void setDeleteFormat(Integer deleteFormat) {
		this.deleteFormat = deleteFormat;
	}

	public String getLanguageCode() {
		return languageCode;
	}

	public void setLanguageCode(String languageCode) {
		this.languageCode = languageCode;
	}

	public List<Languages> getLanguagesList() {
		return languagesList;
	}

	public void setLanguagesList(List<Languages> languagesList) {
		this.languagesList = languagesList;
	}
}